package com.ibiaoke.boss.controller;

import com.ibiaoke.common.base.BaseController;
import com.ibiaoke.common.base.Page;
import com.ibiaoke.common.response.Res;
import com.ibiaoke.common.utils.AssertUtils;
import com.ibiaoke.customer.entity.Customer;
import com.ibiaoke.goods.entity.Goods;
import com.ibiaoke.goods.entity.GoodsClass;
import com.ibiaoke.goods.service.GoodsClassService;
import com.ibiaoke.goods.service.GoodsService;
import com.ibiaoke.order.entity.GoodsRush;
import com.ibiaoke.order.service.GoodsRushService;
import com.ibiaoke.system.entity.Shop;
import com.ibiaoke.system.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Administrator on 2017/6/22.
 */
@Controller
public class WxGoodsController extends BaseController {

    @Autowired
    private GoodsClassService goodsClassService;

    @Autowired
    private ShopService shopService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsRushService goodsRushService;

    /**
     * 查询分类列表
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/listGoodsClass")
    public Res listGoodsClass() {
        List<GoodsClass> goodsClassList = goodsClassService.listGoodsClass();
        return Res.create().addResponse("goodsClassList", goodsClassList);
    }

    /**
     * 根据分类查询商品
     *
     * @param cateId
     * @param shopId
     * @param page   @return
     */
    @ResponseBody
    @RequestMapping("/listGoods")
    public Res listGoodsByCateId(Integer cateId, Integer shopId, Page page) {
        List<Goods> goodsList = goodsService.listGoodsByGoodsClass(cateId, shopId, page);
        goodsList.forEach(goods -> {
            goodsRushService.validateCount(goods,1,null,false);
        });
        return Res.create().addResponse("goodsList", goodsList).addResponse("page", page);
    }

    /**
     * 根据id查询商品详情
     *
     * @param goodsId
     * @param shopId
     * @return
     */
    @ResponseBody
    @RequestMapping("/getGoods")
    public Res getGoods(Integer goodsId, Integer shopId) {
        Customer customer = getLoginUserSafely();
        Integer userId = customer == null ? null : customer.getCustomerId();
        Goods goods = goodsService.getGoodsByGoodsId(goodsId, shopId);
        goodsRushService.validateCount(goods,1,userId,false);
        return Res.create().addResponse("goods", goods);
    }

    /**
     * 通过经纬度来确定商铺然后对商品的库存进行验证
     * @param goodsIds
     * @param longitude
     * @param latitude
     * @return
     */
    @ResponseBody
    @RequestMapping("/validGoods")
    public Res validGoods(@RequestParam(required = false, value = "goodsIds[]") List<Integer> goodsIds, Double longitude, Double latitude) {
        Shop nearestShop = shopService.getNearestShop(longitude, latitude);
        AssertUtils.notNull(nearestShop, "该地址附近没有店铺");
        List<Goods> list = goodsService.listGoods(goodsIds, nearestShop.getShopId());
        list.forEach(goods -> {
            goodsRushService.validateCount(goods,1,null,false);
        });
        return  Res.create().addResponse("goodsList",list);
    }
}